﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WPFSample.CommandTheMenuSample
{
    class CommandTheMenu : Window
    {
        TextBlock text;
        protected MenuItem itemCut, itemCopy, itemPaste, itemDelete;


        public CommandTheMenu()
        {
            this.Title = "Command the Menu";

            DockPanel dock = new DockPanel();
            this.Content = dock;

            Menu menu = new Menu();
            dock.Children.Add(menu);
            DockPanel.SetDock(menu, Dock.Top);

            this.text = new TextBlock()
            {
                Text = "Sample Clipboard text",
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                FontSize = 32,
                TextWrapping = TextWrapping.Wrap
            };
            dock.Children.Add(this.text);

            MenuItem itemEdit = new MenuItem();
            itemEdit.Header = "_Edit";
            menu.Items.Add(itemEdit);

            this.itemCut = new MenuItem();
            this.itemCut.Header = "Cu_t";
            this.itemCut.Command = ApplicationCommands.Cut;
            itemEdit.Items.Add(this.itemCut);

            this.itemCopy = new MenuItem();
            this.itemCopy.Header = "_Copy";
            this.itemCopy.Command = ApplicationCommands.Copy;
            itemEdit.Items.Add(this.itemCopy);

            this.itemPaste = new MenuItem();
            this.itemPaste.Header = "_Paste";
            this.itemPaste.Command = ApplicationCommands.Paste;
            itemEdit.Items.Add(this.itemPaste);

            this.itemDelete = new MenuItem();
            this.itemDelete.Header = "_Delete";
            this.itemDelete.Command = ApplicationCommands.Delete;
            itemEdit.Items.Add(this.itemDelete);

            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut,
                this.CutOnExecute, this.CutCanExecute));
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy,
                this.CopyOnExecute, this.CutCanExecute));
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste,
                this.PasteOnExecute, this.PasteCanExecute));
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Delete,
                this.DeleteOnExecute, this.CutCanExecute));

            InputGestureCollection collGestures = new InputGestureCollection();
            collGestures.Add(new KeyGesture(Key.R, ModifierKeys.Control));
            RoutedUICommand commRestore = new RoutedUICommand("_Restore", "Restore", this.GetType(), collGestures);

            MenuItem itemRestore = new MenuItem();
            itemRestore.Header = "_Restore";
            itemRestore.Command = commRestore;
            itemEdit.Items.Add(itemRestore);
            this.CommandBindings.Add(new CommandBinding(commRestore, this.RestoreExecute));
        }

        private void RestoreExecute(object sender, ExecutedRoutedEventArgs e)
        {
            this.text.Text = "Sample Clipboard text";
        }

        private void DeleteOnExecute(object sender, ExecutedRoutedEventArgs e)
        {
            this.text.Text = null;
        }

        private void PasteOnExecute(object sender, ExecutedRoutedEventArgs e)
        {
            this.text.Text = Clipboard.GetText();
        }

        private void PasteCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = Clipboard.ContainsText();
        }

        private void CopyOnExecute(object sender, ExecutedRoutedEventArgs e)
        {
            if (this.text.Text != null && this.text.Text.Length > 0)
            {
                Clipboard.SetText(this.text.Text);
            }
        }

        private void CutCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = this.text.Text != null && this.text.Text.Length > 0;
        }

        private void CutOnExecute(object sender, ExecutedRoutedEventArgs e)
        {
            ApplicationCommands.Copy.Execute(null, this);
            ApplicationCommands.Delete.Execute(null, this);
        }
    }
}
